package 力扣;

public class 实现strStr {
    public static void main(String[] args) {
         String haystack="apple";
         String needle="le";
        int i = strStr(haystack, needle);
        System.out.println(i);
    }
    public static int strStr(String haystack, String needle) {
         //使用暴力算法进行求解 KMP算法还不能完全理解
        if (needle==null || needle.length()==0){
            return 0;
        }
        int i=haystack.length();
        int j=0;
        int k=0;
        while (j<i && k<needle.length()){
            if (haystack.charAt(j)==needle.charAt(k)){
                j++;
                k++;
            }
            else{
                j=j-k+1;
                k=0;
            }
        }
        if (k==needle.length()){
            return j-k;
        }
        return -1;
    }
}
